<template>
	<view class="">
		<view class="getmyredgift" style="background: rgba(244, 239, 230, 1);">
			<view class="" style="background:url(../../static/image/redbackground.png) no-repeat;background-size: 150% 150%;width: 100%;height: 100%;z-index: 999;position: relative;">
				<view class="title">
					<view class="header">
						<view class="inline-block left">
							<image src="../../static/image/getmyredgiftback.png" mode="" @tap="back" style="width: 66upx;height: 66upx;"></image>
						</view>
						<view class="inline-block titleText" :style="i18n.getredtitleTextstyle">
							{{i18n.getredtitle}}
						</view>
					</view>
					<view class="twolabel">
						<view class="twolabel1" @click="navtypeClick(0)">
							<view class="active" v-if='navtype==0'>
								{{i18n.Gotit}}
							</view>
							<view v-else>
								{{i18n.Gotit}}
							</view>
						</view>
						<view class="twolabel1" @click="navtypeClick(1)">
							<view class="active" v-if='navtype==1'>
								{{i18n.Issued}}
							</view>
							<view v-else>
								{{i18n.Issued}}
							</view>
						</view>
					</view>
				</view>
				<!--列表-->
				<view class="liststyle">
					<!--分页结束-->
					<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltolower="lower" @scroll="scroll">
						<view class="list" v-for="(item,index) in RedEnvelopeList">
							<view class="one">
								<image src="../../static/image/avator.png"></image>
							</view>
							<view class="two">
								<view class="name">
									{{sender}}
								</view>
								<view class="backhongbao" v-if="item.redid==1" @tap="open(index)">
									<text v-if="item.receiveaccount">{{item.receiveaccount}}</text>
									<text v-if="item.remake">{{item.remake}}</text>
								</view>
								<view class="backhongbaopt" v-else @tap="open(index)">
									<text v-if="item.receiveaccount">{{item.receiveaccount}}</text>
									<text v-if="item.remake">{{item.remake}}</text>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>

		</view>
		<view class="mcgrey" :style="mcxs">
			<view class="mcgreybj" @tap="mcgreyClick">
			</view>
			<image src="../../static/image/openhb.png" mode="" @tap="closehongbao"></image>
		</view>
	</view>
</template>
<script>
	import account from '../../common/account.js'
	import * as oex from 'oex-web3';
	import payment from '../../common/payment.js'
	import password from '../../components/password/password.vue'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import CryptoJS from 'crypto-js';
	export default {
		computed: {
			i18n() {
				return this.$t('user')
			}
		},
		data() {
			return {
				sender: "",
				mobile: "",
				phone: "",
				RedEnvelopeList: [],
				//蒙层显示
				mcxs: "display:none",
				txhash: "",
				hongbaoindex: "",
				avator: "",
				navtype: 0,
				pageIndex: 1,
				pageSize: 7,
				name: "",
				sender1: "",
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				jmaccount:"",
				userMobile:""//用户手机号
			}
		},
		onLoad(options) {
			this.sender = options.sender
			this.getUserInfo();
		},
		onShow() {
		
			this.avator = uni.getStorageSync('avator') ? uni.getStorageSync('avator') : '../../static/image/avator.png'
		},
		methods: {
			lower: function(e) { //触底加载更多
				var _this = this
				if (_this.navtype == 0) {
					uni.request({
						url: this.$API.appServeApi + this.$API.getmyredgift + '?i18n_language=' + uni.getStorageSync('system_info'),
						method: "POST",
						header: {
							'content-type': 'application/x-www-form-urlencoded'
						},
						data: {
							pk: _this.sender
						},
						success(res) {
							if (res.data.code == 200) {
								_this.RedEnvelopeList = res.data.data
								uni.hideLoading()
							} else {}
						},
						fail(e) {
							uni.hideLoading()
							_this.$CommonJS.showToast(e)
						}
					})
				} else {
					uni.request({
						url: this.$API.appServeApi + this.$API.getbysender + '?i18n_language=' + uni.getStorageSync('system_info'),
						data: {
							account: _this.sender,
							pageIndex: _this.pageIndex,
							pageSize: _this.pageSize
						},
						success(res) {
							if (res.data.code == 200 && res.data.data.list.length > 0) {
								var list = res.data.data.list
								var n = _this.RedEnvelopeList.concat(list)
								_this.RedEnvelopeList = n
								_this.pageIndex = _this.pageIndex + 1
								uni.hideLoading()
							} else {
								uni.hideLoading()
							}
						},
						fail(e) {
							uni.hideLoading()
							_this.$CommonJS.showToast(e)
						}
					})
				}
			},
			scroll: function(e) {
				this.old.scrollTop = e.detail.scrollTop
			},
			async accountInfo() {
				//加载账户
				const _this = this;
				await account.getAccount().then(res => {
					_this.accountInfomation = res.data.result;
					_this.accountName = res.data.result.accountName;
					_this.accountID = res.data.result.accountID;
					_this.yqmname = res.data.result.invitationcode
					uni.setStorage({
						key: "accountName",
						data: res.data.result.accountName
					})

				}).catch(error => {})
				this.getUserInfo(_this.accountName)
			},
			getUserInfo() {
				//获取手机号
				const _this = this;
				const data = {
					account: _this.sender
				};
				account.getuserInfo(data).then(res => {
					if (res.code == 200) {
						_this.phone = res.data.mobile;
						var data = res.data.mobile
						_this.name = res.data.account
						//对称验证
						var key = CryptoJS.enc.Utf8.parse("QWERTYHUJKOLL34C");
						var srcs = CryptoJS.enc.Utf8.parse(res.data.account);
						var encrypted = CryptoJS.AES.encrypt(srcs, key, {
							mode: CryptoJS.mode.ECB,
							padding: CryptoJS.pad.Pkcs7
						});
							_this.jmaccount=encrypted.toString()
						if (res.data.mobile) {
							uni.request({
								url: this.$API.appServeApi + this.$API.getmyredgift + '?i18n_language=' + uni.getStorageSync('system_info'),
								method: "POST",
								header: {
									'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
								},
								data: {
									pk:encrypted.toString()
								},
								success(res) {
									if (res.data.code == 200 && res.data.data.length > 0) {
										_this.RedEnvelopeList = res.data.data
										
										uni.hideLoading()

									} else {
										uni.hideLoading()
										// _this.$CommonJS.showToast(res.data.message)
									}
								},
								fail(e) {
									uni.hideLoading()
									_this.$CommonJS.showToast(e)
								}
							})
						}
					} else {}

				}).catch(error => {})
			},
			//抢到的红包列表
			getmyredgift() {
				var _this = this
				const data = {
					account: _this.sender
				};
			},
			//返回
			back() {
				this.$CommonJS.navigateBack()
			},
			//打开红包
			open(e) {
				var _this = this
				_this.hongbaoindex = e
				if (_this.navtype == 0) { //抢到的
					this.mcxs = "display:block"
				} else { //发出的
					var rpk = _this.RedEnvelopeList[_this.hongbaoindex].txhash
					var amount = _this.RedEnvelopeList[_this.hongbaoindex].amount
					var sender = _this.RedEnvelopeList[_this.hongbaoindex].sender
					var assetname = _this.RedEnvelopeList[_this.hongbaoindex].assetname
					_this.$CommonJS.navigateTo('../red_detail/red_detail?redinfoid=' + rpk + '&sender=' + sender +
						"&amount=" + amount + "&assetname=" + assetname)
				}
			},
			mcgreyClick() {
				var _this = this
				this.mcxs = "display:none"
			},
			//领取红包
			closehongbao() {
				uni.showLoading({
				})
				var _this = this
				// getAmount
				var redinfoid = _this.RedEnvelopeList[_this.hongbaoindex].txhash
				var rpk = _this.RedEnvelopeList[_this.hongbaoindex].rpk
				var amount = _this.RedEnvelopeList[_this.hongbaoindex].amount
				var assetname = _this.RedEnvelopeList[_this.hongbaoindex].assetname
				var sender = _this.RedEnvelopeList[_this.hongbaoindex].sender
				uni.request({
					url:this.$API.appServeApi + this.$API.mobile + '?i18n_language=' + uni.getStorageSync('system_info'),		
				data:{
					account:_this.jmaccount
				},success(res){
					if(res.data.code==200){
								uni.request({
									url: _this.$API.appServeApi + _this.$API.getAmount + '?i18n_language=' + uni.getStorageSync('system_info'),
									method: "POST",
									header: {
										'content-type': 'application/x-www-form-urlencoded'
									},
									data: {
										pk: res.data.data.mobile,
										redinfoid: redinfoid,
										account: _this.sender
									},
									success(res) {
										if (res.data.code == 200) {
											if (_this.phone) {
												uni.request({
													url: _this.$API.appServeApi + _this.$API.getmyredgift + '?i18n_language=' + uni.getStorageSync('system_info'),
													method: "POST",
													header: {
														'content-type': 'application/x-www-form-urlencoded'
													},
													data: {
														pk: _this.jmaccount
													},
													success(res) {

														if (res.data.code == 200) {
															_this.RedEnvelopeList = res.data.data
															_this.mcxs = "display:none"
															_this.$CommonJS.navigateTo('../red_detail/red_detail?redinfoid=' + rpk + '&sender=' + sender +
																"&amount=" + amount + "&assetname=" + assetname)
								
														} else {
																_this.$CommonJS.showToast(res.data.message)
														}
													},
													fail(e) {
														uni.hideLoading()
														_this.$CommonJS.showToast(e)
													}
												})
											}
										} else {}
									},
									fail(e) {
										uni.hideLoading()
										_this.$CommonJS.showToast(e)
									}
								})
					}
				},fail(e){
					_this.$CommonJS.showToast(e)
				}
				})
		
			},
			navtypeClick(e) {
				var _this = this
				_this.pageIndex = 1
				_this.navtype = e
				if (e == 0) {
					uni.showLoading({

					})
					uni.request({
						url: this.$API.appServeApi + this.$API.getmyredgift + '?i18n_language=' + uni.getStorageSync('system_info'),
						method: "POST",
						header: {
							'content-type': 'application/x-www-form-urlencoded'
						},
						data: {
							pk: _this.jmaccount
						},
						success(res) {
							if (res.data.code == 200) {
								_this.RedEnvelopeList = res.data.data
								uni.hideLoading()

							} else {}
						},
						fail(e) {
							uni.hideLoading()
							_this.$CommonJS.showToast(e)
						}
					})
				} else {
					uni.showLoading({

					})
					uni.request({
						url: this.$API.appServeApi + this.$API.getbysender + '?i18n_language=' + uni.getStorageSync('system_info'),
						data: {
							account: _this.sender,
							pageIndex: this._pageindex,
							pageSize: _this.pageSize
						},
						success(res) {
							if (res.data.code == 200) {
								uni.hideLoading()
								var list = res.data.data.list
								_this.RedEnvelopeList = list
								_this.pageIndex = 2
							} else {}
						},
						fail(e) {
							uni.hideLoading()
							_this.$CommonJS.showToast(e)
						}
					})
				}
			}
		},
	}
</script>
<style lang="stylus">
	@import "../../static/stylus/getmyredgift/getmyredgift.stylus"
	.scroll-Y {
		height: 100%;
	}
</style>
